/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;


import java.io.*;
import java.util.AbstractList;
import javax.servlet.*;
import javax.servlet.http.*;
import negocio.*;

/**
 *
 * @author user
 */
public class Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
       // ServletContext context = this.getServletContext();
       HttpSession s = request.getSession();
       //ManejadorBaseDato bd=new ManejadorBaseDato();
       //Usuario integrante= new Usuario();

       //DESEncryption e=new DESEncryption();


       String opcion = request.getParameter("opcion");
       String user = request.getParameter("txtUser");
       String contraseña = request.getParameter("txtPass");

       //String responsable = request.getParameter("responsable");

       //String estado = request.getParameter("estado");
      // String importancia = request.getParameter("importancia");
       // String tipo = request.getParameter("tipo");
       //  String fechas = request.getParameter("fechas");

         //Tarea nueva= new Tarea( fechas, responsable, estado, tipo, importancia);
     /*  integrante.setUser_int(user);
       integrante.setPassword_int(contraseña);

       //System.out.println(user+" "+ contraseña);
       url = e.devolverURL(context.getInitParameter("url"));
       String usr=e.devolverURL(context.getInitParameter("usr"));
       String pas=e.devolverURL(context.getInitParameter("pas"));
       System.out.println("desencriptada "+url+","+usr+",+pas");
       // System.out.println("encriptada: "+context.getInitParameter("url"));
*/

        /*****************************cambiar la lista**********************/

     if(opcion.equalsIgnoreCase("login")){

           String rol = (String) request.getAttribute("rol");
           //cambiar por metodo
           //String nombreUsuario=integrante.obtenerIdentidad(url,usr,pas);
            AbstractList opcionesMenu;

            Menu menuUsuario = new Menu();

            if(rol.equalsIgnoreCase("owner")){
                opcionesMenu = menuUsuario.menuAdministrador();
                s.setAttribute("menuMiembro", opcionesMenu);
                s.setAttribute("cargo",rol);
                //s.setAttribute("nombre",nombreUsuario);
                RequestDispatcher rd1 = getServletContext().getRequestDispatcher("/index.jsp");
                rd1.forward(request, response);
            }
            if(rol.equalsIgnoreCase("member")){
                opcionesMenu = menuUsuario.menuUser();
                s.setAttribute("menuMiembro", opcionesMenu);
                s.setAttribute("cargo",rol);
                //s.setAttribute("nombre",nombreUsuario);
                RequestDispatcher rd1 = getServletContext().getRequestDispatcher("/index.jsp");
                rd1.forward(request, response);
            }



    }else if(opcion.equalsIgnoreCase("cerrarSesion")){
            s.invalidate();
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
            rd.forward(request, response);
         }else if(opcion.equalsIgnoreCase("Wish-List")){
            //nueva.crearTarea(url, usr, pas);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/wishlist.jsp");
            rd.forward(request, response);
         }
       /*else if(opcion.equalsIgnoreCase("crearTarea")){
            //nueva.crearTarea(url, usr, pas);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/exito.jsp");
            rd.forward(request, response);
         }*/
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
